package com.stoneframe.util.baseFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileHelper {
	
	/**
	 * 创建目录
	 */
	public static boolean createDir(String path){
		File dir=new File(path);
		if(dir.exists()){
			return false;
		}
		if(!path.endsWith(File.separator)){
			path=path+File.separator;
		}
		if(dir.mkdirs()){
			return true;
		}
		return false;
	}
	
	/**
	 * 读取字节数组
	 */
	public static byte[] toByteArray(String filePath) throws Exception{
		File file=new File(filePath);
		if(!file.exists()){
			throw new FileNotFoundException(filePath);
		}
		FileChannel channel=null;
		FileInputStream fs=null;
		try{
			fs=new FileInputStream(file);
			channel=fs.getChannel();
			ByteBuffer byteBuffer=ByteBuffer.allocate((int)channel.size());
			return byteBuffer.array();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			try{
				channel.close();
			}catch(IOException e){
				e.printStackTrace();
			}
			try{
				fs.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 复制文件
	 */
	public static String copyFile(InputStream inputStream,String dir,String realName)
		throws IOException{
		File file=new File(dir,realName);
		if(!file.exists()){
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileUtils.copyInputStreamToFile(inputStream, file);
		return realName;
	}
	
	/**
	 * 下载文件
	 */
	public static void fileDownload(final HttpServletResponse response,String filePath,String fileName) 
			throws Exception{
		byte[] data =toByteArray(filePath);
		fileName=URLEncoder.encode(fileName,"UTF-8");
		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");  
	    response.addHeader("Content-Length", "" + data.length);  
	    response.setContentType("application/octet-stream;charset=UTF-8"); 
	    OutputStream outputStream=new BufferedOutputStream(response.getOutputStream());
	    outputStream.write(data);
	    outputStream.flush();
	    outputStream.close();
	    response.flushBuffer();
	}
	
	/**
	 * 上传文件
	 */
	public static String fileUpload(MultipartFile file,String filePath,String fileName){
		String extName="";
		try{
			if(file.getOriginalFilename().lastIndexOf(".")>=0){
				extName=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			}
			copyFile(file.getInputStream(), filePath, fileName+extName);
		}catch(Exception e){
			e.printStackTrace();
		}
		return fileName+extName;
	}

}
